package com.shen.thread.syn;

public class UnsafeBank {
    public static void main(String[] args) {
        Account account = new Account(1000, "基金");
        Drawing drawing1 = new Drawing(account, 500, "you");
        Drawing drawing2 = new Drawing(account, 800, "friend");
        drawing1.start();
        drawing2.start();
    }
}

//账户
class Account {
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

//银行:模拟取款
class Drawing extends Thread {
    Account account;
    int drawingMoney;
    int nowMoney;

    public Drawing(Account account, int drawingMoney, String name) {
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;

    }

    @Override
    public void run() {
        synchronized (account) {


            //判断有没有钱
            if (account.money - drawingMoney < 0) {
                System.out.println(Thread.currentThread().getName() + "money不够");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //卡内余额
            account.money = account.money - drawingMoney;
            nowMoney = nowMoney + drawingMoney;
            System.out.println(account.name + "余额为" + account.money);
            System.out.println(Thread.currentThread().getName() + "手里的钱" + nowMoney);

        }
    }
}
